home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / database / int.c < prev    next >
Text File  |  1984-05-21  |  5KB  |  224 lines

  1. /* SDB - boolean expression evaluator */
  2.  
  3. #include "sdbio.h"
  4.  
  5. static struct operand *stack[STACKMAX],**sptr;
  6. static union codecell *cptr;
  7.  
  8. /* db_interpret - interpret a boolean expression */
  9. int db_interpret(slptr)
  10.   struct sel *slptr;
  11. {
  12.     struct operand *result;
  13.     int r;
  14.  
  15.     /* check for empty where clause */
  16.     if ((cptr = slptr->sl_where) == NULL)
  17.         return (TRUE);
  18.  
  19.     /* setup stack */
  20.     sptr = stack;
  21.  
  22.     /* execute the code */
  23.     while ((*(*cptr++).c_operator)())
  24.         ;
  25.  
  26.     /* get the result from the top of stack */
  27.     result = *--sptr;
  28.     r = result->o_value.ov_boolean;
  29.     if (result->o_type == TEMP)
  30.         free(result);
  31.  
  32.     /* make sure the stack is empty */
  33.     while (sptr != stack) {
  34.         if ((*sptr)->o_type == TEMP)
  35.             free(*sptr);
  36.         sptr -= 1;
  37.     }
  38.  
  39.     /* return result */
  40.     return (r);
  41. }
  42.  
  43. int db_xstop()
  44. {
  45.     return (FALSE);
  46. }
  47.  
  48. int db_xpush()
  49. {
  50.     *sptr++ = (*cptr++).c_operand;
  51. }
  52.  
  53. int db_xand()
  54. {
  55.     return (boolean('&'));
  56. }
  57.  
  58. int db_xor()
  59. {
  60.     return (boolean('|'));
  61. }
  62.  
  63. static int boolean(opr)
  64. {
  65.     struct operand *lval,*rval,*result;
  66.     int lv,rv,r;
  67.  
  68.     rval = *--sptr; lval = *--sptr;
  69.     lv = lval->o_value.ov_boolean;
  70.     rv = rval->o_value.ov_boolean;
  71.  
  72.     if ((result = malloc(sizeof(struct operand))) == NULL)
  73.         return (db_ferror(INSMEM));
  74.     result->o_type = TEMP;
  75.     switch (opr) {
  76.     case '&':   r = (lv && rv);
  77.                 break;
  78.     case '|':   r = (lv || rv);
  79.                 break;
  80.     }
  81.     result->o_value.ov_boolean = r;
  82.     *sptr++ = result;
  83.     if (lval->o_type == TEMP)
  84.         free(lval);
  85.     if (rval->o_type == TEMP)
  86.         free(rval);
  87.     return (TRUE);
  88. }
  89.  
  90. int db_xnot()
  91. {
  92.     struct operand *val,*result;
  93.  
  94.     val = *--sptr;
  95.     if ((result = malloc(sizeof(struct operand))) == NULL)
  96.         return (db_ferror(INSMEM));
  97.     result->o_type = TEMP;
  98.     result->o_value.ov_boolean = !val->o_value.ov_boolean;
  99.     *sptr++ = result;
  100.     if (val->o_type == TEMP)
  101.         free(val);
  102.     return (TRUE);
  103. }
  104.  
  105. int db_xlss()
  106. {
  107.     return (compare(LSS));
  108. }
  109.  
  110. int db_xleq()
  111. {
  112.     return (compare(LEQ));
  113. }
  114.  
  115. int db_xeql()
  116. {
  117.     return (compare(EQL));
  118. }
  119.  
  120. int db_xgeq()
  121. {
  122.     return (compare(GEQ));
  123. }
  124.  
  125. int db_xgtr()
  126. {
  127.     return (compare(GTR));
  128. }
  129.  
  130. int db_xneq()
  131. {
  132.     return (compare(NEQ));
  133. }
  134.  
  135. static int compare(cmp)
  136. {
  137.     struct operand *lval,*rval,*result;
  138.     int i;
  139.  
  140.     rval = *--sptr; lval = *--sptr;
  141.     if ((result = malloc(sizeof(struct operand))) == NULL)
  142.         return (db_ferror(INSMEM));
  143.     result->o_type = TEMP;
  144.  
  145.     if (lval->o_value.ov_char.ovc_type == TCHAR)
  146.         i = comp(lval,rval);
  147.     else
  148.         i = ncomp(lval,rval);
  149.  
  150.     switch (cmp) {
  151.     case LSS:   i = (i < 0);
  152.                 break;
  153.     case LEQ:   i = (i <= 0);
  154.                 break;
  155.     case EQL:   i = (i == 0);
  156.                 break;
  157.     case GEQ:   i = (i >= 0);
  158.                 break;
  159.     case GTR:   i = (i > 0);
  160.                 break;
  161.     case NEQ:   i = (i != 0);
  162.                 break;
  163.     }
  164.     result->o_value.ov_boolean = i;
  165.     *sptr++ = result;
  166.     if (lval->o_type == TEMP)
  167.         free(lval);
  168.     if (rval->o_type == TEMP)
  169.         free(rval);
  170.     return (TRUE);
  171. }
  172.  
  173. static int comp(lval,rval)
  174.   struct operand *lval,*rval;
  175. {
  176.     char *lptr,*rptr; int lctr,rctr;
  177.     int len;
  178.  
  179.     lptr = lval->o_value.ov_char.ovc_string;
  180.     lctr = lval->o_value.ov_char.ovc_length;
  181.     rptr = rval->o_value.ov_char.ovc_string;
  182.     rctr = rval->o_value.ov_char.ovc_length;
  183.  
  184.     while (lctr > 0 && (lptr[lctr-1] == 0 || lptr[lctr-1] == ' '))
  185.         lctr--;
  186.     while (rctr > 0 && (rptr[rctr-1] == 0 || rptr[rctr-1] == ' '))
  187.         rctr--;
  188.  
  189.     if (lctr < rctr)
  190.         len = lctr;
  191.     else
  192.         len = rctr;
  193.  
  194.     while ((len--) > 0) {
  195.         if (*lptr++ != *rptr++)
  196.             if (*--lptr < *--rptr)
  197.                 return (-1);
  198.             else
  199.                 return (1);
  200.     }
  201.  
  202.     if (lctr == rctr)
  203.         return (0);
  204.     else if (lctr < rctr)
  205.         return (-1);
  206.     else
  207.         return (1);
  208. }
  209.  
  210. static int ncomp(lval,rval)
  211.   struct operand *lval,*rval;
  212. {
  213.     char lstr[NUMBERMAX+1],rstr[NUMBERMAX+1];
  214.     int len;
  215.  
  216.     stccpy(lstr,lval->o_value.ov_char.ovc_string,
  217.           (len = lval->o_value.ov_char.ovc_length)); lstr[len] = EOS;
  218.     stccpy(rstr,rval->o_value.ov_char.ovc_string,
  219.           (len = rval->o_value.ov_char.ovc_length)); rstr[len] = EOS;
  220.  
  221.     return (db_cmp(lstr,rstr));
  222. }
  223.  
  224.